package com.wwm.petadopt;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class AccountFragment extends Fragment {
    private static android.app.FragmentManager fragmentManager;
    TextView textviewAdoptionRecord;
    Button btnLogOut;
    TextView textviewEmail;
    TextView textviewGender;
    TextView textviewConstellation;
    TextView textviewBloodType;
    TextView textviewShortMSG;
    private RequestQueue mRequestQueue;
    UserInfo userinfo;
    @Override
    public void onStart() {
        super.onStart();
        ActionBar ab = getActivity().getActionBar();
        if (ab != null) {
            View actionbarview = getActivity().getLayoutInflater().inflate(R.layout.actionbar, null);
            ((TextView) actionbarview.findViewById(R.id.text_actionbar)).setText(R.string.account);

            ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            ab.setCustomView(actionbarview);
            Log.i("actionbar", "AccountFragment");
        }
    }
    public void getAdoptRecordInfo(View view)
    {
        mRequestQueue = Volley.newRequestQueue(view.getContext());
        Map<String, String> map = new HashMap<String, String>();
        map.put("User_Name", GlobalData.username);

        JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, GlobalData.USER_ADOPT_INFO_URL, new JSONObject(map), new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {
                Log.i("petadopt", response.toString());


                AdoptionRecord adoptionRecord=new AdoptionRecord(response);
                textviewAdoptionRecord.setText(adoptionRecord.getAdoptionRecord());
            }
        }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                Log.i("petadopt", error.getMessage());
            }
        });
        mRequestQueue.add(jr);
    }
    public void getUserInfo(View view)
    {
        mRequestQueue = Volley.newRequestQueue(view.getContext());
        Map<String, String> map = new HashMap<String, String>();
        map.put("User_Name", GlobalData.username);

        JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, GlobalData.GET_USER_INFO_URL, new JSONObject(map), new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {
                Log.i("petadopt", response.toString());

               userinfo=new UserInfo(response);

                textviewEmail.setText(userinfo.e_mail);
                textviewGender.setText(userinfo.gender);
                textviewConstellation.setText(userinfo.constellation);
                textviewBloodType.setText(userinfo.bloodtype);
                textviewShortMSG.setText(userinfo.shortmessage);

            }
        }, new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error)
            {
                Log.i("petadopt", error.getMessage());
            }
        });
        mRequestQueue.add(jr);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        View view = inflater.inflate(R.layout.fragment_account, container, false);

        fragmentManager = getActivity().getFragmentManager();

        final Button btnRegisterLogin = (Button) view.findViewById(R.id.button_register_login);
        textviewEmail=(TextView)view.findViewById(R.id.text_email);
         textviewGender=(TextView)view.findViewById(R.id.text_gender);
         textviewConstellation=(TextView)view.findViewById(R.id.text_constellation);
         textviewBloodType=(TextView)view.findViewById(R.id.text_bloodtype);
         textviewShortMSG=(TextView)view.findViewById(R.id.text_shortmessage);
         textviewAdoptionRecord=(TextView)view.findViewById(R.id.textview_adoption_record);
         textviewAdoptionRecord.setMovementMethod(ScrollingMovementMethod.getInstance());
         btnLogOut=(Button)view.findViewById(R.id.button_logout);
        if (GlobalData.isLogin == true) {
            btnRegisterLogin.setText(GlobalData.username);
            textviewEmail.setVisibility(View.VISIBLE);
            textviewGender.setVisibility(View.VISIBLE);
            textviewConstellation.setVisibility(View.VISIBLE);
            textviewBloodType.setVisibility(View.VISIBLE);
            textviewShortMSG.setVisibility(View.VISIBLE);
            textviewAdoptionRecord.setVisibility(View.VISIBLE);
            btnLogOut.setVisibility(View.VISIBLE);
            getAdoptRecordInfo(view);
            getUserInfo(view);

        }
        btnLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GlobalData.isLogin=false;
                btnRegisterLogin.setText(R.string.login);
                GlobalData.username="";
                textviewAdoptionRecord.setText("");
                textviewEmail.setText("");
                textviewGender.setText("");
                textviewConstellation.setText("");
                textviewBloodType.setText("");
                textviewShortMSG.setText("");

                textviewEmail.setVisibility(View.GONE);
                textviewGender.setVisibility(View.GONE);
                textviewConstellation.setVisibility(View.GONE);
                textviewBloodType.setVisibility(View.GONE);
                textviewShortMSG.setVisibility(View.GONE);
                textviewAdoptionRecord.setVisibility(View.GONE);

                btnLogOut.setVisibility(View.GONE);
            }
        });

        btnRegisterLogin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (GlobalData.isLogin == false)
                {
                    Intent intent = new Intent("android.intent.action.LOGIN");
                    startActivity(intent);
                }
                else if (GlobalData.isLogin == true)
                {
                    //Logout
                }
            }
        });

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);


    }
}
